#include "qopenharmonycapturesession.h"

QT_BEGIN_NAMESPACE

QOPenHarmonyCaptureSession::QOPenHarmonyCaptureSession(QOPenHarmonyCameraSession *cameraSession)
    : QObject()
      , m_cameraSession(cameraSession)
      , m_duration(0)
      , m_state(QMediaRecorder::StoppedState)
      , m_status(QMediaRecorder::UnloadedStatus)
      , m_containerFormat()
      , m_containerFormatDirty(true)
{

}

QOPenHarmonyCaptureSession::~QOPenHarmonyCaptureSession()
{
    stop();
}

qint64 QOPenHarmonyCaptureSession::duration() const
{
    return m_duration;
}

QMediaRecorder::State QOPenHarmonyCaptureSession::state() const
{
    return m_state;
}

QMediaRecorder::Status QOPenHarmonyCaptureSession::status() const
{
    return m_status;
}

void QOPenHarmonyCaptureSession::setState(QMediaRecorder::State state)
{
    if (m_state == state)
        return;

    switch (state) {
    case QMediaRecorder::StoppedState:
        stop();
        break;
    case QMediaRecorder::RecordingState:
        start();
        break;
    case QMediaRecorder::PausedState:
        // Not supported by Android API
        qWarning("QMediaRecorder::PausedState is not supported on Android");
        break;
    }
}

void QOPenHarmonyCaptureSession::setContainerFormat(const QString &format)
{
    if (m_containerFormat == format)
        return;

    m_containerFormat = format;
    m_containerFormatDirty = true;
}

void QOPenHarmonyCaptureSession::updateDuration()
{
    if (m_elapsedTime.isValid())
        m_duration = m_elapsedTime.elapsed();

    emit durationChanged(m_duration);
}

void QOPenHarmonyCaptureSession::onCameraOpened()
{

}

void QOPenHarmonyCaptureSession::onError(int what, int extra)
{
    Q_UNUSED(what)
    Q_UNUSED(extra)
    stop(true);
    emit error(QMediaRecorder::ResourceError, QLatin1String("Unknown error."));
}

void QOPenHarmonyCaptureSession::onInfo(int what, int extra)
{
    Q_UNUSED(extra)
    if (what == 800) {
        // MEDIA_RECORDER_INFO_MAX_DURATION_REACHED
        setState(QMediaRecorder::StoppedState);
        emit error(QMediaRecorder::OutOfSpaceError, QLatin1String("Maximum duration reached."));
    } else if (what == 801) {
        // MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED
        setState(QMediaRecorder::StoppedState);
        emit error(QMediaRecorder::OutOfSpaceError, QLatin1String("Maximum file size reached."));
    }
}

void QOPenHarmonyCaptureSession::start()
{
    m_elapsedTime.start();
    m_notifyTimer.start();
    updateDuration();

    m_state = QMediaRecorder::RecordingState;
    emit stateChanged(m_state);
    setStatus(QMediaRecorder::RecordingStatus);
}

void QOPenHarmonyCaptureSession::stop(bool error)
{

}

void QOPenHarmonyCaptureSession::setStatus(QMediaRecorder::Status status)
{
    if (m_status == status)
        return;

    m_status = status;
    emit statusChanged(m_status);
}

void QOPenHarmonyCaptureSession::updateViewfinder()
{

}

void QOPenHarmonyCaptureSession::restartViewfinder()
{

}

QT_END_NAMESPACE
